package com.tz.actions.user;

import com.opensymphony.xwork2.ActionSupport;
import com.tz.entity.User;
import com.tz.service.IUserService;
import com.tz.util.MyAcUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 * Created by Administrator on 2016/10/8 0008.
 */
@Namespace("/")
public class UserRegisterAction extends ActionSupport {

    private User user;

    private IUserService userService =
            MyAcUtil.getInstance().getBean(IUserService.class);

    public void validateExecute(){

        if(StringUtils.isBlank(user.getUsername())){
            addFieldError("username","用户名不能为空");
        }
        if(StringUtils.isBlank(user.getPassword())){
            addFieldError("password","密码不能为空");
        }


    }


    @Action(value = "register",
            results = {@Result(type = "redirectAction",location = "index"),
                    @Result(name = "input",location = "/myaccount.jsp")
            })
    public String execute() {

        userService.register(user);

        return SUCCESS;

    }


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
